package cn.stronglink.asset.message.mq.dam.handle;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;

import cn.stronglink.asset.message.mq.dam.entity.IScreenCommandFactory;
import cn.stronglink.asset.message.mq.dam.entity.RackCodeEntity;
import cn.stronglink.asset.message.mq.dam.entity.ScreenCommandEntity;
import cn.stronglink.asset.message.mq.dam.entity.dacai.UpdateArrayTextCommandFactory;
import cn.stronglink.asset.message.mq.dam.message.MQMessageOfDAM;
import cn.stronglink.asset.message.mq.topic.ReaderInfoSender;
import cn.stronglink.asset.model.RfidState;
import cn.stronglink.asset.module.dam.entity.dev.AssetInfo;
import cn.stronglink.asset.module.dam.service.DamService;
import cn.stronglink.asset.module.dam.service.IDamService;
import cn.stronglink.suite.core.util.ContextUtils;

public class OpenBindRfidPageHandle extends AbstractScreenCommandHandle{
	protected final static String TOPIC_NAME = "serviceToDam";
	protected final static String ACTION_CODE = "screenRequest";
	protected final static String SEND_ACTION_CODE= "screenCommand";
	protected final static String UPDATE_BIND_RFID_MENU= "updateBindRfidMenu";
	protected IDamService damService =(DamService)ContextUtils.getBean("sensorService");
	private ReaderInfoSender topicSender = (ReaderInfoSender) ContextUtils.getBean("readerInfoSender");
	private final static short SCREEN_ID = 10;
	@Override
	protected String getCommandCode() {
		return "EEB11100070002100100";
	}

	@Override
	protected void handleMessage(MQMessageOfDAM msg) {
		RackCodeEntity entity = (RackCodeEntity) msg.getAwsPostdata();
		if(entity!=null) {
			List<RfidState> rfidStateList = damService.qryRfidStateByRackConverCode(entity.getRackConverCode());
			List<AssetInfo> assetList = damService.qryAssetListByRackConverCode(entity.getRackConverCode());
			if(assetList!=null&&assetList.size()>0) {
				sendAssetToDam(entity.getRackConverCode(),assetList);
			}
			if(rfidStateList!=null){
				if(assetList!=null&&assetList.size()>0) {
					for(RfidState r :rfidStateList) {
						for(AssetInfo assetInfo :assetList) {
							if(assetInfo.getStartU()!=null&&r.getU()!=null&&assetInfo.getStartU()==r.getU()) {
								r.setAssetId(String.valueOf(assetInfo.getId()));
								r.setAssetName(assetInfo.getAssetName());
								r.setuHeight(assetInfo.getuHeight()); 
							}
						}
					}
				}
				sendRfidStateToDam(entity.getRackConverCode(),rfidStateList);
			}
		}
	}

	private void sendAssetToDam(String rackConverCode, List<AssetInfo> assetList) {
		Map<String,Object> map = new HashMap<>();
		map.put("assetList", assetList);
		map.put("deviceAddress", rackConverCode);
		
		MQMessageOfDAM message = new MQMessageOfDAM();
		message.setActioncode(UPDATE_BIND_RFID_MENU);
		message.setAwsPostdata(map);
		message.setTimestamp(System.currentTimeMillis());
		
		String json = JSON.toJSONString(message);
		topicSender.send(TOPIC_NAME,json);
		logger.info("topic:{}  发送命令:{}",TOPIC_NAME,json);
	}

	private void sendRfidStateToDam(String rackConverCode,List<RfidState> rfidStateList) {
		try {
			Map<Short, String> map = new HashMap<>();
			for(int i=0;i<24;i++) {
				if(i>rfidStateList.size()-1) {
					map.put((short)(5*i+1), " ");
					map.put((short)(5*i+2), " ");
					map.put((short)(5*i+3), " ");
					map.put((short)(5*i+4), " ");
					map.put((short)(5*i+5), " ");
				}else {
					RfidState rfidState =  rfidStateList.get(i);
					map.put((short)(5*i+1),rfidState.getAssetId()==null?" ":rfidState.getAssetId());
					map.put((short)(5*i+2), rfidState.getRfidNo()==null?" ":rfidState.getRfidNo());
					map.put((short)(5*i+3), rfidState.getAssetName()==null?" ":rfidState.getAssetName());
					map.put((short)(5*i+4),rfidState.getU()==null? " ":String.valueOf(rfidState.getU()));
					map.put((short)(5*i+5), rfidState.getuHeight()==null?" ":rfidState.getuHeight().toString());
				}
				sendCommand(rackConverCode,map);
				map.clear();
			}
			
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void sendCommand(String rackConverCode, Map<Short, String> map) throws Exception {
		IScreenCommandFactory rackScreenCommandFactroy = new UpdateArrayTextCommandFactory(rackConverCode, SCREEN_ID, map);
		ScreenCommandEntity sc =  rackScreenCommandFactroy.createScreenCommand();
		
		MQMessageOfDAM message = new MQMessageOfDAM();
		message.setActioncode(SEND_ACTION_CODE);
		message.setAwsPostdata(sc);
		message.setTimestamp(System.currentTimeMillis());
		
		String json = JSON.toJSONString(message);
		topicSender.send(TOPIC_NAME,json);
		logger.info("topic:{}  发送命令:{}",TOPIC_NAME,json);
	}
}
